import sys

import os
from PyQt5.QtCore import QObject, QUrl, pyqtSlot
from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication

from action.addFriendAction import AddFriendAction
from action.commonAction import CommonAction
from action.contactAction import ContactAction
from action.mainAction import MainAction
from action.msgAction import MsgAction
from cache import Cache


class App(QObject):

    def __init__(self):
        super().__init__()

    def start(self,sys):

        sys.argv.append("--remote-debugging-port=9222")

        app = QApplication(sys.argv)
        self.qApplication = app

        channel = QWebChannel()
        from action.loginAction import LoginAction
        loginAction = LoginAction()
        channel.registerObject('loginObject', loginAction)
        self.loginView = QWebEngineView()
        self.loginView.setFixedWidth(300)
        self.loginView.setFixedHeight(400)
        self.loginView.page().setWebChannel(channel)
        self.loginView.load(QUrl('file://'+ os.getcwd() + '/ui/html/login.html'))
        self.loginView.show()

        sys.exit(app.exec_())

    @pyqtSlot()
    def createMain(self):
        channel = QWebChannel()
        contactObject = ContactAction()
        channel.registerObject('contactObject', contactObject)
        self.mainView = QWebEngineView()
        self.mainView.setMinimumHeight(600)
        self.mainView.setMinimumWidth(900)
        self.mainView.page().setWebChannel(self.mainChannel)
        self.mainView.load(QUrl('file://'+ os.getcwd() + '/ui/html/main.html'))
        self.mainView.hide()

        #self.view.raise_()
        # QMessageBox.information(None,"test","test")
        # self.app.processEvents()

    @pyqtSlot()
    def showMain(self):
        self.mainView.show()
        self.loginView.close()
        self.loginView = None


    @pyqtSlot()
    def createAddFriendView(self):
        self.addFriendView = QWebEngineView()
        self.addFriendView.setFixedHeight(200)
        self.addFriendView.setFixedWidth(300)
        self.addFriendView.page().setWebChannel(self.addFriendChannel)
        self.addFriendView.load(QUrl('file://' + os.getcwd() + '/ui/html/addFriend.html'))
        self.addFriendView.show()



if __name__ == '__main__':

    app = App()

    mainChannel = QWebChannel()
    contactObject = ContactAction()
    commonObject = CommonAction()
    mainObject = MainAction()
    msgObject = MsgAction()
    mainChannel.registerObject('contactObject', contactObject)
    mainChannel.registerObject('commonObject',commonObject)
    mainChannel.registerObject('mainObject',mainObject)
    mainChannel.registerObject('msgObject',msgObject)
    app.mainChannel = mainChannel
    app.contactObject = contactObject
    app.commonObject = commonObject
    app.mainObject = mainObject
    app.msgObject = msgObject

    addFriendChannel = QWebChannel()
    addFriendObject = AddFriendAction()
    addFriendChannel.registerObject('addFriendObject', addFriendObject)
    app.addFriendChannel = addFriendChannel

    Cache.app = app
    app.start(sys)









